/*
 * Copyright © 2010 SenseForce.com.
 * The Software shall be used for Good, not Evil.
 */

package com.senseforce.classes;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;

/**
 *
 * @author jcccn
 * @site http://www.senseforce.com/
 */
public class MyCheckItem extends CustomItem implements ItemCommandListener {
    private final static int UPPER = 0;
    private final static int IN  = 1;
    private final static int LOWER = 2;
    //组件间遍历使用
    private int status = UPPER;
    //组件间遍历使用
    private int location = 1;
    //当前选项
    private int checked = 1;
    private Image img1;
    private Image img2;
    private final static Command CMD_Update = new Command("Check", Command.ITEM, 1);
    
    public MyCheckItem(String title, String src1, String src2) {
        super(title);
        //读取图像文件
        try {
            img1 = Image.createImage(src1);
            img2 = Image.createImage(src2);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        setDefaultCommand(CMD_Update);
        setItemCommandListener(this);
    }

    protected int getMinContentHeight() {
        //获取图像的高度
        int h1 = img1.getHeight();
        int h2 = img2.getHeight();
        if (h1 >= h2) {
            return 2 * h1 + 10;
        }
        else {
            return 2 * h2 + 10;
        }
    }

    protected int getMinContentWidth() {
        int w1 = img1.getWidth();
        int w2 = img2.getWidth();
        if (w1 >= w2) {
            return w1 + 30;
        }
        else {
            return w2 + 30;
        }
    }

    protected int getPrefContentHeight(int width) {
       int h1 = img1.getHeight();
       int h2 = img2.getHeight();
       if (h1 >= h2) {
           return 2 * h1 + 20;
       }
       else {
           return 2 * h2 + 20;
       }
    }

    protected int getPrefContentWidth(int height) {
       int w1 = img1.getWidth();
       int w2 = img2.getWidth();
       if (w1 >= w2) {
           return w1 + 50;
       }
       else {
           return w2 + 50;
       }
    }

    protected void paint(Graphics g, int w, int h) {
        int height = getMinContentWidth();
        int width = getMinContentHeight();
        g.setStrokeStyle(Graphics.DOTTED);
        g.drawRect(0, 0, width, height);
        g.setStrokeStyle(Graphics.SOLID);
        g.drawArc(4, 4, 13, 13, 0, 360);
        g.drawArc(4, height/2+5, 13, 13, 0, 360);
        if (checked == 1) {
            g.fillArc(4, 4, 13, 13, 0, 360);
        }
        else {
            g.fillArc(4, height/2+5, 13, 13, 0, 360);
        }
        if (location == 1) {
            g.drawRect(2, 2, width-5, height/2-5);
        }
        else {
            g.drawRect(2, height/2, width-5, height/2-5);
        }
        g.drawImage(img1, 22, 1, Graphics.TOP|Graphics.LEFT);
        g.drawImage(img2, 22, height/2, Graphics.TOP|Graphics.LEFT);
    }

    protected boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout) {
        switch(dir) {
            case Canvas.DOWN:
                if (status == UPPER) {
                    status = IN;
                }
                else {
                    if (location == 1) {
                        location = 2;
                    }
                    else if (location == 2) {
                        return false;
                    }
                }
                repaint();
                break;
            case Canvas.UP:
                if (status == LOWER) {
                    status = IN;
                }
                else {
                    if (location == 2) {
                        location = 1;
                    }
                    else if (location == 1) {
                        return false;
                    }
                }
                repaint();;
                break;
        }
        return true;
    }

    public void commandAction(Command c, Item item) {
        if (c == CMD_Update) {
            checked = location;
            repaint();
            notifyStateChanged();
        }
    }
    public int getState() {
        return checked;
    }

}
